﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Script.Serialization;
using Omu.AwesomeWebFormsDemo.Model;

namespace Omu.AwesomeWebFormsDemo.AjaSrv
{
    public class SelectListItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
        public bool Selected { get; set; }
    }

    public class IdContent
    {
        public string Id { get; set; }
        public string Content { get; set; }
    }

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Aja
    {
        #region Fruits
        static readonly IList<Fruit> Fruits = new List<Fruit>
                                                 {
                                                     new Fruit{Id=1, Name = "Mango"},
                                                     new Fruit{Id=2, Name = "Apple"},
                                                     new Fruit{Id=3, Name = "Papaya"},
                                                     new Fruit{Id=4, Name = "Banana"},
                                                     new Fruit{Id=5, Name = "Cherry"},
                                                 };
        [WebGet]
        [OperationContract]
        public IEnumerable<IdContent> FruitSelected(string selected)
        {
            var ser = new JavaScriptSerializer();
            var sel = ser.Deserialize<int[]>(selected);
            return Fruits.Where(o => sel.Contains(o.Id))
                .Select(f => new IdContent { Id = f.Id.ToString(), Content = f.Name });
        }

        [WebGet]
        [OperationContract]
        public IEnumerable<IdContent> FruitSearch(string search, string selected)
        {
            var ser = new JavaScriptSerializer();
            var sel = ser.Deserialize<int[]>(selected);
            return Fruits.Where(o => o.Name.StartsWith(search) && !sel.Contains(o.Id))
                .Select(f => new IdContent { Id = f.Id.ToString(), Content = f.Name });
        }


        [WebGet]
        [OperationContract]
        public IEnumerable<IdContent> FruitGetMultiple(string keys)
        {
            var ids = new JavaScriptSerializer().Deserialize<int[]>(keys);
            return Fruits.Where(o => ids.Contains(o.Id)).Select(f => new IdContent { Id = f.Id.ToString(), Content = f.Name });
        }
        #endregion

    }
}
